package com.cy.pj.sys.web.advice;

import com.cy.pj.sys.web.pojo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice  //==@ControllerAdvice +@ResponseBody
@Slf4j  //作为错误的日志记录
public class GlobalExceptionHander {
    @ExceptionHandler(NumberFormatException.class)
    public JsonResult doHandleNumberFormatException(NumberFormatException e) {
        e.printStackTrace(); //控制台打印
        log.error("exception {}","你传如参数值类型不匹配");

        //return new JsonResult(e);//封装
//        JsonResult r = new JsonResult();
//        r.setState(0);
//        r.setMessage("你传如参数值类型不匹配");
//        return r;
        return new JsonResult(0,"你传如参数值类型不匹配");
    }
    //异常处理方法
    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandleRuntimeException(RuntimeException e) {
        e.printStackTrace(); //控制台打印
        log.error("exception {}",e.getMessage());
        return new JsonResult(e);//封装
    }
}
